package com.phonebills.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.phonebills.service.UserService;
import com.phonebills.service.impl.UserServiceImpl;
import com.phonebills.vo.Bills;
import com.phonebills.vo.Page;
import com.phonebills.vo.User;


/**
 * 处理器(注解方式)
 * @author LIU
 * @version 1.0 2017-10-26
 */
@Controller
public class PhoneController {

	/**
	 * 创建业务逻辑对象
	 */
	UserService userService = new UserServiceImpl();
	
	/**
	 * 用户登陆
	 *TODO
	 *LIU
	 * @param uname
	 * @param pwd
	 * @return ModelAndView
	 * @throws Exception
	 *下午4:29:52
	 */
	@RequestMapping("login.action")
	public ModelAndView login(HttpServletRequest request,@RequestParam("uname") String uname, @RequestParam("pwd") String pwd) throws Exception {
		// TODO Auto-generated method stub
		ModelAndView mav = null;
		User user = new User();
		user.setNo(uname);
		user.setPassword(pwd);

		if(userService.login(user)){
			mav = new ModelAndView("showAll.action");
			User currUser = userService.getCurrUser(uname);
			request.getSession().setAttribute("user", currUser );
		}else{
			mav = new ModelAndView("login.jsp");
			mav.addObject("mess","登陆失败！");
		}
		
		return mav;
	}
	
	
	/**
	 * 分页显示信息
	 *TODO
	 *LIU
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 *下午4:40:32
	 */
	@RequestMapping("showAll.action")
	public ModelAndView showAll(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		ModelAndView mav = null;
		Page page = new Page();
		page.setCurrCount(3);
		page.setTotalCount(userService.getAllCounts());
		User user = (User) request.getSession().getAttribute("user");
		char type = user.getType();
		
		if(request.getParameter("curr") == null){
			mav = new ModelAndView("showAll.jsp");
			page.setCurrPage(1);
			List<Bills> billsList = userService.showAll(page);
			for (int i = 0; i < billsList.size(); i++) {
				billsList.get(i).setTotalAmount(userService.calTotalAmount(type, billsList.get(i)));
			}
			request.setAttribute("billsList", billsList);
			request.setAttribute("page", page);
			request.getRequestDispatcher("showAll.jsp").forward(request, response);
		}else{
			mav = new ModelAndView("currList.jsp");
			page.setCurrPage(Integer.parseInt(request.getParameter("curr")));
			List<Bills> billsList = userService.showAll(page);
			for (int i = 0; i < billsList.size(); i++) {
				billsList.get(i).setTotalAmount(userService.calTotalAmount(type, billsList.get(i)));
			}
			request.setAttribute("billsList", billsList);
			request.setAttribute("page", page);
			request.getRequestDispatcher("currList.jsp").forward(request, response);
		}
	
		return mav;
	}
	
	
	
	
	
	
	
}
